﻿using System.Collections.Generic;
using CYZHAPI.IRepository;
using CYZHAPI.IRepository.IService;
using CYZHAPI.Common.tools;
using CYZHAPI.Entity;
using Newtonsoft.Json;

namespace CYZHAPI.Repository.Service
{
    public class UserLoginService : IUserLoginService
    {
        private IUserLoginRep m_UserLoginRep; 
        public UserLoginService(IUserLoginRep rep)
        {
            m_UserLoginRep = rep;
        }
        
        public cookieinfo get(string sName, string sPassword, string randomValue)
        {
            cookieinfo objjson = new cookieinfo();
            string strMd5Password = cryptionTool.GetMd5String(sPassword);
            UserModel obj = m_UserLoginRep.get(sName, strMd5Password);
            List<string> objList = m_UserLoginRep.getRol(sName);
            /**
              *这里的obj 可能为空，例如用户密码错误时，查询结果返回null
              *by wyf
             */
            if(obj==null){
                objjson.code = "1001";
                objjson.cookieValue = "";
                return objjson;
            }
            if (strMd5Password.Equals(obj.mm))
            {
                string s = obj.mm.Substring(1, 8);
                string s_vc = JsonConvert.SerializeObject(objList);
                s_vc = cryptionTool.DesEncrypt(s_vc);
                string str_cookie = obj.yhm + "|"+randomValue+'|' + s_vc;
                objjson.code = "200";
                objjson.cookieValue = str_cookie;
            }
            else
            {
                objjson.code = "401";
                objjson.cookieValue = "";
            }
            //string json1 = JsonConvert.SerializeObject(objjson);  
            return objjson;
        }
    }
}